package org.example.demo;

import java.sql.*;

public abstract class CompareDatabaseUtil {

    public CompareResult compareTable( UrlParam sourceParam, UrlParam targetParam, String table) {
        boolean flag = this.checkParam(sourceParam, targetParam, table);
        if (!flag) {
            throw new IllegalArgumentException("参数校验不合法！");
        }
        Connection source = this.initConnection(sourceParam);
        Connection target = this.initConnection(targetParam);
        CompareResult compareResult = this.analyzeTable(source, target, table);
        //释放资源
        try {
            if (!source.isClosed()) {
                source.close();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            if (!source.isClosed()) {
                target.close();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return compareResult;
    }

    protected abstract Connection initConnection(UrlParam url);

    protected abstract CompareResult analyzeTable(Connection source, Connection target, String tableName);

    protected boolean checkParam(UrlParam sourceUrl, UrlParam targetUrl, String table) {
        return true;
    }

    /*public static void main(String[] args) throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.cj.jdbc.Driver");

        Connection conn = DriverManager.getConnection("jdbc:mysql://rm-wz93hlx1o3eb1y1643o.mysql.rds.aliyuncs.com:3306/my_test",
                "root","Wjs159357");


        *//*PreparedStatement ps = conn.prepareStatement("select * from ?");
        ps.setString(1,"stu");
        ResultSet resultSet = ps.executeQuery();*//*
        Statement stmt = conn.createStatement();
        ResultSet resultSet = stmt.executeQuery("show index from " + "stu");

        while(resultSet.next()) {
            System.out.println(resultSet.getString("Key_name") + " " + resultSet.getString("Column_name"));
        }

        conn.close();
    }*/
}
